package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.AskGetMoneyModel;
import com.cheer.ba.model.BankModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 邀請返利
 *
 * @author HQ
 *         created at 2017/3/3 0003 15:50
 */
public class AskQuestPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
    }

    @Override
    public void detachView() {

    }

    /**
     * 邀请分费获利额度
     */
    public void getAskMoney(boolean isShowLoad) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData<AskGetMoneyModel>> call = mUserService.getInvitationMoney(2002, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<AskGetMoneyModel>>() {
            @Override
            public void onResponse(Call<ResponseData<AskGetMoneyModel>> call, Response<ResponseData<AskGetMoneyModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<AskGetMoneyModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        AskGetMoneyModel rules = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(rules, ConstantTag.GET_ASK_MONEY);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.search_fail));
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.search_fail));
            }

            @Override
            public void onFailure(Call<ResponseData<AskGetMoneyModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.search_fail));
            }
        });
    }

    /**
     * 邀请好友列表
     */
    public void getFriendList(boolean isShowLoad) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        }
        Call<ResponseData<List<AskGetMoneyModel>>> call = mUserService.getAskFriends(2002, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<AskGetMoneyModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<AskGetMoneyModel>>> call, Response<ResponseData<List<AskGetMoneyModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<AskGetMoneyModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        LogUtils.LogD("测试", "000------");
                        List<AskGetMoneyModel> friends = res.getData();
                        if (null != friends && !friends.isEmpty()) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(friends, ConstantTag.GET_MY_FRIENDS);
                        } else {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL);
                        }
                        LogUtils.LogD("测试", friends.size() + "------");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                LogUtils.LogD("测试", "111------");
            }

            @Override
            public void onFailure(Call<ResponseData<List<AskGetMoneyModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                LogUtils.LogD("测试", "222------");
            }
        });
    }

    /**
     * 邀请银行卡列表
     */
    public void getBankList(boolean isShowLoad) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        }
        Call<ResponseData<List<BankModel>>> call = mUserService.getBankList(2002, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<BankModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<BankModel>>> call, Response<ResponseData<List<BankModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<BankModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<BankModel> banks = res.getData();
                        if (null != banks) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(banks, ConstantTag.GET_MY_BANKS);
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                LogUtils.LogD("测试", "111------");
            }

            @Override
            public void onFailure(Call<ResponseData<List<BankModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
                LogUtils.LogD("测试", "222------");
            }
        });
    }

    /**
     * 提现
     */
    public void getMoney(boolean isShowLoad, String bankNo, String money) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.getMoney(2002, Session.getUserId(), bankNo, money);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag("", ConstantTag.GET_MONEY_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "提现失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "提现失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "提现失败");
            }
        });
    }

    /**
     * 删除银行卡
     */
    public void delMyBank(boolean isShowLoad, String bankNo) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.delBank(2002, Session.getUserId(), bankNo);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag("", ConstantTag.DEL_MY_BANKS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "删除失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "删除失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "删除失败");
            }
        });
    }

    /**
     * 添加银行卡
     */
    public void addMyBank(boolean isShowLoad, String phone, String bankNo, String bankName, String veriCode, String bankAddress) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.addBank(2002, Session.getUserId(), phone, bankNo, bankName, veriCode, bankAddress);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag("", ConstantTag.ADD_MY_BANKS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "添加失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "添加失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "添加失败");
            }
        });
    }

    @Override
    public Object getCacheData() {
        return null;
    }
}
